package lazy2.util;

import java.lang.reflect.Array;
import java.lang.reflect.Method;

public class DataUtil {
	private static Method parseIntMethod;
	private static Method parseLongMethod;
	private static Method parseDoubleMethod;
	private static Method valueOfIntegerMethod;
	private static Method valueOfLongMethod;
	private static Method valueOfDoubleMethod;
	
	static {
		Class<?> cls = DataUtil.class;
		try {
			parseIntMethod       = cls.getDeclaredMethod("parseInt",       String.class);
			parseLongMethod      = cls.getDeclaredMethod("parseLong",      String.class);
			parseDoubleMethod    = cls.getDeclaredMethod("parseDouble",    String.class);
			valueOfIntegerMethod = cls.getDeclaredMethod("valueOfInteger", String.class);
			valueOfLongMethod    = cls.getDeclaredMethod("valueOfLong",    String.class);
			valueOfDoubleMethod  = cls.getDeclaredMethod("valueOfDouble",  String.class);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
	}
	
	public static int     parseInt(String s)       throws NumberFormatException {return Integer.parseInt(s);}
	public static long    parseLong(String s)      throws NumberFormatException {return Long.parseLong(s);}
	public static double  parseDouble(String s)    throws NumberFormatException {return Double.parseDouble(s);}
	public static Integer valueOfInteger(String s) throws NumberFormatException {return Integer.valueOf(s);}
	public static Long    valueOfLong(String s)    throws NumberFormatException {return Long.valueOf(s);}
	public static Double  valueOfDouble(String s)  throws NumberFormatException {return Double.valueOf(s);}
	
	private static boolean tryParse(String s, Object outValue, int index, Method method) {
		if(outValue!=null && index>=0 && index<Array.getLength(outValue)
		&& s!=null && (s=s.trim()).length()>0) {
			try {
				Array.set(outValue, index, method.invoke(DataUtil.class, s));
				return true;
			} catch (NumberFormatException e) {
			} catch (Exception e) {}
		}
		return false;
	}
	private static boolean tryParse(String[] s, Object outValue, Method method) {
		boolean res = true;
		if(outValue!=null && s!=null) {
			for(int i=0,len=Math.min(Array.getLength(outValue), s.length);i<len;i++) {
				try {
					Array.set(outValue, i, method.invoke(DataUtil.class, s[i]));
				} catch (NumberFormatException e) {
					res = false;
				} catch (Exception e) {
					res = false;
				}
			}
		}
		return res;
	}
	public static boolean tryParse(String   s, int[]     outValue, int index) {return tryParse(s, outValue, index, parseIntMethod);}
	public static boolean tryParse(String   s, long[]    outValue, int index) {return tryParse(s, outValue, index, parseLongMethod);}
	public static boolean tryParse(String   s, double[]  outValue, int index) {return tryParse(s, outValue, index, parseDoubleMethod);}
	public static boolean tryParse(String   s, Integer[] outValue, int index) {return tryParse(s, outValue, index, valueOfIntegerMethod);}
	public static boolean tryParse(String   s, Long[]    outValue, int index) {return tryParse(s, outValue, index, valueOfLongMethod);}
	public static boolean tryParse(String   s, Double[]  outValue, int index) {return tryParse(s, outValue, index, valueOfDoubleMethod);}
	public static boolean tryParse(String   s, int[]     outValue)            {return tryParse(s, outValue, 0, parseIntMethod);}
	public static boolean tryParse(String   s, long[]    outValue)            {return tryParse(s, outValue, 0, parseLongMethod);}
	public static boolean tryParse(String   s, double[]  outValue)            {return tryParse(s, outValue, 0, parseDoubleMethod);}
	public static boolean tryParse(String   s, Integer[] outValue)            {return tryParse(s, outValue, 0, valueOfIntegerMethod);}
	public static boolean tryParse(String   s, Long[]    outValue)            {return tryParse(s, outValue, 0, valueOfLongMethod);}
	public static boolean tryParse(String   s, Double[]  outValue)            {return tryParse(s, outValue, 0, valueOfDoubleMethod);}
	public static boolean tryParse(String[] s, int[]     outValue)            {return tryParse(s, outValue, parseIntMethod);}
	public static boolean tryParse(String[] s, long[]    outValue)            {return tryParse(s, outValue, parseLongMethod);}
	public static boolean tryParse(String[] s, double[]  outValue)            {return tryParse(s, outValue, parseDoubleMethod);}
	public static boolean tryParse(String[] s, Integer[] outValue)            {return tryParse(s, outValue, valueOfIntegerMethod);}
	public static boolean tryParse(String[] s, Long[]    outValue)            {return tryParse(s, outValue, valueOfLongMethod);}
	public static boolean tryParse(String[] s, Double[]  outValue)            {return tryParse(s, outValue, valueOfDoubleMethod);}
}
